package client;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;

import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import communication.ClientComm;
import communication.ComandoComm;

public class GraphicButtonController implements ActionListener {
	private ClientModel client;
	private ClientComm clientComm;
	private GraphicViewRightPanel right;
	private ComandoComm tipoAzione;
	private JTextField info1;
	private JTextField info2;
	private String info2content;
	//------costruttori per ogni bottone: bottone con campo di testo, con due campi di testo ------
	public GraphicButtonController(ComandoComm c, ClientModel cl, ClientComm cm, GraphicViewRightPanel r){
		tipoAzione = c;
		client = cl;
		clientComm = cm;
		right = r;
		info1 = null;
		info2 = null;
	}
	public GraphicButtonController(ComandoComm c, ClientModel cl, ClientComm cm, GraphicViewRightPanel r, JTextField text1){
		tipoAzione = c;
		client = cl;
		clientComm = cm;
		right = r;
		info1 = text1;
		info2 = null;
	}	
	public GraphicButtonController(ComandoComm c, ClientModel cl, ClientComm cm, GraphicViewRightPanel r, JTextField text1, JTextField text2){
		tipoAzione = c;
		client = cl;
		clientComm = cm;
		right = r;
		info1 = text1;
		info2 = text2;
	}
	
	public void setInfo2(String info) {
		this.info2content = info;
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		try {			
			switch(tipoAzione){
			case login: 			login(); 			break;
			case creaUtente:		creaUtente();		break;
			case creaRazza:			creaRazza(); 		break;
			case accessoPartita: 	accessoPartita(); 	break;
			case logout: 			logout(); 			break;
			case passaTurno:		passaTurno();		break;
			case cresciDinosauro:	cresciDinosauro();	break;
			case deponiUovo:		deponiUovo();		break;
			}
		} catch (IllegalArgumentException e){
			e.printStackTrace();
		}
	}

	public void login() {
		if (info1 == null || info2 == null)
			return;
		String[] usPw = new String[2];
		usPw[0] = info1.getText();
		if (info2 instanceof JPasswordField) {
			JPasswordField pass = (JPasswordField)info2;
			usPw[1] = pass.getPassword().toString();
		} else {
			usPw[1] = info2.getText();
		}
		client.setGiocatore(usPw[0], usPw[1]);
		try {
			clientComm.sendRequest(ComandoComm.login.toString() + "," + usPw[0] + "," + usPw[1]);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}

	public void creaUtente() {
		if (info1 == null || info2 == null)
			return;
		String[] usPw = new String[2];
		usPw[0] = info1.getText();
		if (usPw[0].length()==0) {
			JOptionPane.showMessageDialog(null, "Devi inserire uno username!", "Attento!", JOptionPane.ERROR_MESSAGE);
			return;
		}	
		if (info2 instanceof JPasswordField) {
			JPasswordField pass = (JPasswordField)info2;
			usPw[1] = pass.getPassword().toString();
		} else {
			usPw[1] = info2.getText();
		}
		client.setGiocatore(usPw[0], usPw[1]);
		try {
			clientComm.sendRequest(ComandoComm.creaUtente.toString() + "," + usPw[0] + "," + usPw[1]);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}

	public void creaRazza() {
		if (info1!=null && info2content!=null) {
			String raz = info1.getText();
			client.getGameStatus().setNomeSpecie(raz, info2content.charAt(0));
			try {
				clientComm.sendRequest(ComandoComm.creaRazza.toString() + "," + client.getToken() + "," + raz + "," + info2content);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
	}

	public void accessoPartita() {
		try {
			clientComm.sendRequest(ComandoComm.accessoPartita.toString() + "," + client.getToken());
			client.getGameStatus().askedStateBeforeFight = true;
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}

	public void uscitaPartita() {
		try {
			clientComm.sendRequest(ComandoComm.uscitaPartita.toString() + "," + client.getToken());
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}
	
	public void logout() {
		try {
			clientComm.sendRequest(ComandoComm.logout.toString() + "," + client.getToken());
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}

	public void cresciDinosauro() {
		if (client.getGameStatus().isMyTurn && client.getGameStatus().selectedDino) {
			try {
				clientComm.sendRequest(ComandoComm.cresciDinosauro.toString() + "," + client.getToken() + "," + client.getGameStatus().idDinoSelected);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
			client.getGameStatus().idDinoAzionato = client.getGameStatus().idDinoSelected;
		}
	}

	public void deponiUovo() {
		if (client.getGameStatus().isMyTurn && client.getGameStatus().selectedDino) {
			try {
				clientComm.sendRequest(ComandoComm.deponiUovo.toString() + "," + client.getToken() + "," + client.getGameStatus().idDinoSelected);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
			client.getGameStatus().idDinoAzionato = client.getGameStatus().idDinoSelected;
		}
	}

	public void passaTurno() {
		right.removeButtonsForDino();
		if (client.getGameStatus().isMyTurn)
			try {
				clientComm.sendRequest(ComandoComm.passaTurno.toString() + "," + client.getToken());
			} catch (RemoteException e) {
				e.printStackTrace();
			}
	}
}
